import { message } from "ant-design-vue";
import Axios, { AxiosRequestConfig } from "axios";
// import router from '@/router/index'
import { useRouter } from "vue-router";
const router = useRouter();
let axiosIns = Axios.create({
  timeout: 60 * 1000,
  headers: {
    "Cache-Control": "no-cache",
    "X-Requested-With": "XMLHttpRequest",
  },
  // baseURL:'http://192.168.99.69:80/'
});
// axiosIns.defaults.baseURL = 'http://127.0.0.1:3006';
axiosIns.defaults.headers.post["X-Requested-With"] = "XMLHttpRequest";
// axiosIns.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest';
axiosIns.defaults.responseType = "json";
// axiosIns.defaults.baseURL = 'http:127.0.0.1:8080'
axiosIns.defaults.validateStatus = function () {
  return true;
};
// http response interceptor
axiosIns.interceptors.response.use(
  async (response) => {
    return response;
  },
  (error) => {
    if (error.message.includes("timeout")) {
      if (localStorage.getItem("lang") === "en") {
        error.data = "Timeout";
      } else {
        error.data = "请求超时";
      }
    }
    return Promise.reject(error);
  }
);

export default {
  axiosIns,
};
/**post
 * @param options
 * @return {Promise}
 */
export function post(options: AxiosRequestConfig<any>) {
  return new Promise((resolve, reject) => {
    axiosIns(options)
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}
/**get
 * @param options
 * @return {Promise}
 */
export function get(options: AxiosRequestConfig<any>) {
  return new Promise((resolve, reject) => {
    axiosIns(options)
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

/**patch
 * @param options
 * @return {Promise}
 */
export function patch(options: AxiosRequestConfig<any>) {
  return new Promise((resolve, reject) => {
    axiosIns(options)
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}
/**put
 * @param options
 * @return {Promise}
 */
export function put(options: AxiosRequestConfig<any>) {
  return new Promise((resolve, reject) => {
    axiosIns(options)
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}
/**delete
 * @param options
 * @return {Promise}
 */
export function deleted(options: AxiosRequestConfig<any>) {
  console.log("options", options);
  return new Promise((resolve, reject) => {
    axiosIns(options)
      .then((response) => {
        resolve(response);
      })
      .catch((error) => {
        reject(error);
      });
  });
}
